package cn.w.outdoor.lib;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

import cn.w.outdoor.lib.common.CommonMethods;
import cn.w.outdoor.lib.model.Intercom_Msg;
import cn.w.outdoor.lib.model.PCMSG;
import cn.w.outdoor.lib.network.TCPTools;

public class IntercomSystem {

    public interface IntercomCallback {

    }

    private static final IntercomSystem ourInstance = new IntercomSystem();

    public static IntercomSystem getInstance() {
        return ourInstance;
    }

    private IntercomSystem() {

        TCPTools.getInstance().addDataListener(new TCPTools.OnDataListener() {
            @Override
            public void onData(String remoteIpAddr, byte[] data) {
                Intercom_Msg intercom_msg = new Intercom_Msg(data);
                synchronized (control) {
                    for (IntercomSystemCallBack intercomSystemCallBack : intercomSystemCallBacks) {
                        if (intercomSystemCallBack != null) {
                            switch (intercom_msg.getuCmd()) {
                                case Intercom_Msg.MSG_INTERCOM_CALL:
                                    intercomSystemCallBack.onCall(remoteIpAddr);
                                    break;
                                case Intercom_Msg.MSG_INTERCOM_RINGING:
                                    intercomSystemCallBack.onRinging(remoteIpAddr);
                                    break;
                                case Intercom_Msg.MSG_INTERCOM_ACCEPT:
                                    intercomSystemCallBack.onAccept(remoteIpAddr);
                                    break;
                                case Intercom_Msg.MSG_INTERCOM_HANGUP:
                                    intercomSystemCallBack.onHangUp(remoteIpAddr);
                                    break;
                                case Intercom_Msg.MSG_INTERCOM_BUSY:
                                    intercomSystemCallBack.onBusy(remoteIpAddr);
                                    break;
                                case Intercom_Msg.MSG_INTERCOM_TIMEOUT:
                                    intercomSystemCallBack.onTimeout(remoteIpAddr);
                                    break;
                                case Intercom_Msg.MSG_INTERCOM_UNLOCK:
                                    intercomSystemCallBack.onUnlock(remoteIpAddr);
                                    break;
                            }
                        }
                    }
                }
            }
        });

    }

    /**
     * 呼叫
     */
    public int call(String ipAddr) {
        return TCPTools.getInstance().sendIntercomMSGTo(new Intercom_Msg(Intercom_Msg.MSG_INTERCOM_CALL), ipAddr);
    }

    /**
     *
     */
    public void ringing(String ipAddr) {
        TCPTools.getInstance().sendIntercomMSGTo(new Intercom_Msg(Intercom_Msg.MSG_INTERCOM_RINGING), ipAddr);
    }

    /**
     * 接听
     */
    public void accept(String ipAddr) {
        TCPTools.getInstance().sendIntercomMSGTo(new Intercom_Msg(Intercom_Msg.MSG_INTERCOM_ACCEPT), ipAddr);
    }

    /**
     * 挂断
     */
    public int hangup(String ipAddr) {
        return TCPTools.getInstance().sendIntercomMSGTo(new Intercom_Msg(Intercom_Msg.MSG_INTERCOM_HANGUP), ipAddr);
    }

    /**
     * 忙
     */
    public void busy(String ipAddr) {
        TCPTools.getInstance().sendIntercomMSGTo(new Intercom_Msg(Intercom_Msg.MSG_INTERCOM_BUSY), ipAddr);
    }

    public void timeOut(String ipAddr) {
        TCPTools.getInstance().sendIntercomMSGTo(new Intercom_Msg(Intercom_Msg.MSG_INTERCOM_TIMEOUT), ipAddr);
    }

    /**
     * 开锁
     */
    public void unlock(String ipAddr) {
        TCPTools.getInstance().sendIntercomMSGTo(new Intercom_Msg(Intercom_Msg.MSG_INTERCOM_UNLOCK), ipAddr);
    }

    public void message(String ipAddr, String message) {
        TCPTools.getInstance().sendPCMSGTo(new PCMSG(message), ipAddr);
    }

    private final AtomicBoolean control = new AtomicBoolean(false);

    private final List<IntercomSystemCallBack> intercomSystemCallBacks = new CopyOnWriteArrayList<>();

    public void addIntercomSystemCallBack(IntercomSystemCallBack intercomSystemCallBack) {
        if (intercomSystemCallBack == null) return;
        synchronized (control) {
            if (!intercomSystemCallBacks.contains(intercomSystemCallBack)) {
                intercomSystemCallBacks.add(intercomSystemCallBack);
            }
        }
    }

    public void removeIntercomSystemCallBack(IntercomSystemCallBack intercomSystemCallBack) {
        if (intercomSystemCallBack == null) return;
        synchronized (control) {
            intercomSystemCallBacks.remove(intercomSystemCallBack);
        }
    }

    public interface IntercomSystemCallBack {

        /**
         * 收到 呼叫指令，远端设备件事时，也会发送此指令
         */
        void onCall(String remoteIpAddr);

        /**
         * 响铃
         */
        void onRinging(String remoteIpAddr);

        /**
         * 接通
         */
        void onAccept(String remoteIpAddr);

        /**
         * 挂断
         */
        void onHangUp(String remoteIpAddr);

        /**
         * 忙
         */
        void onBusy(String remoteIpAddr);

        void onTimeout(String remoteIpAddr);

        /**
         * 开锁
         */
        void onUnlock(String remoteIpAddr);
    }

}
